package com.ziqikeji.concurrent.client;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @author dpf
 */
public class RequestThread implements Runnable {

    private String threadName;

    public RequestThread(int index) {
        this.threadName = "thread：" + index;
    }

    @Override
    public void run() {
        RestTemplate restTemplate = new RestTemplate();

        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("threadName", threadName);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity<MultiValueMap<String, Object>> req = new HttpEntity<>(postParameters, headers);

        String result = restTemplate.postForObject("http://localhost:8080/concurrent/index", req, String.class);
        System.out.println(threadName + "消费，剩余资源数：" + result);
    }
}
